package com.jy.api.controller.admin;

import com.jy.api.dao.MemberAccountDao;
import com.jy.api.dao.StoreOrderDao;
import com.jy.api.domain.StoreOrder;
import com.jy.api.util.Contens;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lihu on 2018/1/16.
 */

@Slf4j
@RestController
@RequestMapping(value = "v1/admin/", produces = "application/json;charset=utf-8")
public class StoreOrderController {


    @Autowired
    StoreOrderDao storeOrderDao;


    @Autowired
    MemberAccountDao memberAccountDao;


    /**
     * 店铺付款接口轮询
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/query/store-orders")
    public ResponseEntity createStoreOrders(@RequestParam(name = "id") String id) {
        StoreOrder storeOrder = storeOrderDao.findOne(id);
        if (storeOrder == null) {
            return new ResponseEntity("查询失败！支付信息不存在！", HttpStatus.BAD_REQUEST);
        }
        if (storeOrder.isSuccess()) {
            return new ResponseEntity(HttpStatus.OK);
        } else {
            return new ResponseEntity(HttpStatus.ACCEPTED);
        }

    }

    /**
     * 获取店铺会员总数和预计支付金额
     *
     * @param storeId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/estimate/store-orders")
    public ResponseEntity estimateStoreOrders(@RequestParam(name = "storeId") String storeId) {
        Object o = memberAccountDao.getStoreAllCount(storeId);
        int memberCount = Integer.parseInt(o.toString());
        Map map = new HashMap();
        map.put("memberCount", memberCount);

        map.put("amount", memberCount * Contens.WECHAT_PRICE);
        return new ResponseEntity(map, HttpStatus.OK);
    }


    /**
     * 获取店铺支付记录
     *
     * @param storeId
     * @param pageable
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{storeId}/store-orders")
    public ResponseEntity getStoreOrders(@PathVariable String storeId,
                                         @PageableDefault(value = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable) {
        return new ResponseEntity(storeOrderDao.findByStoreIdAndSuccessOrderByUpdateTimeDesc(storeId, true, pageable), HttpStatus.OK);
    }

}
